/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package validators;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.imageio.ImageIO;

/**
 *
 * @author Felipe Duarte
 */
public class Util {
    public static String FORMATO_ERROR_LISTA= "border-top-style: ridge;border-bottom-style: ridge;border-left-style: ridge; border-right-style: ridge;border-top-color: #cc0000;border-bottom-color: #cc0000;border-left-color: #cc0000;border-right-color: #cc0000;background-color: #ffcccc;";
    public static String FORMATO_ERROR_CAMPO= "border-top-style: solid;border-bottom-style: ridge;border-left-style: ridge; border-right-style: ridge;border-top-color: #cc0000;border-bottom-color: #cc0000;border-left-color: #cc0000;border-right-color: #cc0000;background-color: #ffcccc;";
    public static String FORMATO_ANCHO_CAMPO300= "width: 300px";
    
    public static BufferedImage getImgReporte(String s) throws IOException {
        BufferedImage imagenReporte = null;
        imagenReporte = ImageIO.read(new File(s + "_img\\agregar.png"));
        return imagenReporte;
    }

    public boolean validarCorreoRegex(String correo) {
        Pattern p = Pattern.compile("^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}$");
        Matcher m = p.matcher(correo);
        return m.matches();
    }

    public boolean esNumericoRegex(String numero) {
        Pattern p = Pattern.compile("[0-9]+");
        Matcher m = p.matcher(numero);
        return m.matches();
    }

    public boolean esCadenaRegex(String cadena) {
        Pattern p = Pattern.compile("[a-zA-ZáéíóúÁÉÍÓÚ]+");
        Matcher m = p.matcher(cadena);
        return m.matches();
    }

    public void mensajeError(String sumario, String detalle) {
        FacesContext context = FacesContext.getCurrentInstance();
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, sumario, detalle);
        context.addMessage(null, message);
    }
    
    public void mensajeExito(String sumario, String detalle) {
        FacesContext context = FacesContext.getCurrentInstance();
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, sumario, detalle);
        context.addMessage(null, message);
    }

    public boolean tieneMensajes() {
        FacesContext context = FacesContext.getCurrentInstance();
        return context.getMessages().hasNext();
    }

    public  int conteoDias(java.util.Date fecha_inicial, java.util.Date fecha_final, boolean dia_final) {
        Calendar cal_inicio = calendarDate(fecha_inicial);
        Calendar cal_fin = calendarDate(fecha_final);
        return conteoDias(cal_inicio, cal_fin, dia_final);
    }

    public static int conteoDias(java.util.Calendar cal_inicial, java.util.Calendar cal_final, boolean dia_final) {
        Calendar cal_inicio = calendarDate(cal_inicial);
        Calendar cal_fin = calendarDate(cal_final);
        if (cal_inicio.before(cal_fin) || cal_inicio.equals(cal_fin)) {
            long dia_Inicial = cal_inicio.getTime().getTime();
            long dia_Final = cal_fin.getTime().getTime();
            int diferencia = (int) Math.floor((dia_Final - dia_Inicial) / (1000 * 60 * 60 * 24));
            if (dia_final) {
                diferencia++;
            }
            return diferencia;
        } else {
            return 0;
        }
    }

    public static java.util.Calendar calendarDate(java.util.Date fecha) {
        Calendar cal_int = Calendar.getInstance();
        cal_int.setTime(fecha);
        cal_int.set(java.util.Calendar.HOUR_OF_DAY, 0);
        cal_int.set(java.util.Calendar.MINUTE, 0);
        cal_int.set(java.util.Calendar.SECOND, 0);
        cal_int.set(java.util.Calendar.MILLISECOND, 0);
        return cal_int;
    }

    public static java.util.Calendar calendarDate(java.util.Calendar cal) {
        java.util.Calendar cal_int = (java.util.Calendar) cal.clone();
        cal_int.set(java.util.Calendar.HOUR_OF_DAY, 0);
        cal_int.set(java.util.Calendar.MINUTE, 0);
        cal_int.set(java.util.Calendar.SECOND, 0);
        cal_int.set(java.util.Calendar.MILLISECOND, 0);
        return cal_int;
    }

    public boolean verificarCampoNumerico(String cadena) {
        try {
            Integer.parseInt(cadena);
            return true;
        } catch (NumberFormatException nfe) {
            return false;
        }
    }

    public String quitarEspacio(String palabra) {
        String x = palabra;
        String y;
        y = x.trim();
        y = x.replaceAll(" ", "");
        return y;
    }
    
    public String quitarBlanCos(String blanco) {
        String retorno = quitarEspacio(blanco);
        return retorno;
    }

   

    
}
